package com.dycx.api.controller.app.user;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dycx.common.RestResponse;
import com.dycx.common.ValidationGroup;
import com.dycx.common.annotation.WriterLog;
import com.dycx.common.enums.SourceEnum;
import com.dycx.message.dto.SendVerificationCodeDTO;
import com.dycx.message.service.VerificationCodeService;
import com.dycx.user.dto.UserDTO;
import com.dycx.user.entity.UserDO;
import com.dycx.user.service.UserAuthService;
import com.dycx.user.service.UserService;
import com.rabbitmq.client.Return;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping(value = "/app/user/login")
public class LoginApiController {
    private UserAuthService userAuthService;
    private VerificationCodeService verificationCodeService;
    private UserService userService;

    /**
     * 账号密码登录
     **/
    @WriterLog
    @PostMapping("passwordLogin")
    public RestResponse<?> login(@RequestBody @Validated(ValidationGroup.PasswordLogin.class) UserDTO dto) {
        return RestResponse.success(userAuthService.login(dto));
    }

    /**
     * 短信登录
     **/
    @WriterLog
    @PostMapping("smsLogin")
    public RestResponse<?> smsLogin(@RequestBody @Validated(ValidationGroup.MobileCode.class) UserDTO dto) {
        return RestResponse.success(userAuthService.smsLogin(dto));
    }


    /**
     * 微信登录
     **/
    @WriterLog
    @GetMapping("wxLogin")
    public RestResponse<String> wxLogin(String code, @RequestHeader("source") Integer source) {
        if (SourceEnum.WX_MP.getCode().equals(source)) {
            return userAuthService.wxMpLogin(code);
        } else if (SourceEnum.WX_MINI_APP.getCode().equals(source)) {
            return userAuthService.wxMiniLogin(code);
        } else if (SourceEnum.ANDROID_APP.getCode().equals(source) || SourceEnum.IOS_APP.getCode().equals(source)) {
            return userAuthService.wxAppLogin(code);
        }

        return RestResponse.fail("未知操作来源，登录失败");
    }

    /**
     * 注册
     **/
    @WriterLog
    @PostMapping("register")
    public RestResponse<String> register(@RequestBody @Validated(ValidationGroup.Insert.class) UserDTO dto) {
        return userAuthService.register(dto) ? RestResponse.success() : RestResponse.fail();
    }

    /**
     * 发送验证码
     **/
    @PostMapping("sendVerificationCode")
    public RestResponse<String> sendVerificationCode(@RequestBody @Valid SendVerificationCodeDTO dto) {
        return verificationCodeService.sendVerificationCode(dto);
    }

    /**
     * 发送注册验证码
     **/
    @PostMapping("sendRegVerificationCode")
    public RestResponse<String> sendRegVerificationCode(@RequestBody @Valid SendVerificationCodeDTO dto) {
        LambdaQueryWrapper<UserDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserDO::getMobile, dto.getMobile());
        UserDO userDO = userService.getOne(queryWrapper);

        Assert.isNull(userDO, "手机号已存在，请更换手机号！");

        return verificationCodeService.sendVerificationCode(dto);
    }
}
